<template>
  <div>
    <n-card :bordered="false" class="proCard">
      <div class="pb-5">
        <h2 style="font-size: 16px; font-weight: bold">车间列表</h2>
        <p class="mt-2"
          >灭火器总数量：<span style="color: #6a4bff; font-size: 19px">{{total}}</span></p
        >
      </div>
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row: ListData) => row.id"
        ref="actionRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1500"
        @edit-end="editEnd"
        @edit-change="onEditChange"
      >
      </BasicTable>
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref, onMounted } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getTableList } from '@/api/table/list';
  import { columns, ListData } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { useRouter, useRoute } from 'vue-router';
  import { type FormRules } from 'naive-ui';
  import {getWorkshopList}from '@/api/api'
  const editEnd = () => {
    console.log(2);
  };
  const onEditChange = () => {
    console.log(2);
  };

  const schemas: FormSchema[] = [
    {
      field: 'name',
      component: 'NInput',
      label: '单位名称',
      componentProps: {
        placeholder: '请输入单位名称',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];
  const route = useRoute();
  const router = useRouter();
  const formRef: any = ref(null);
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    name: '',
    role: null,
    droit: true,
  });
 
  
  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          {
            label: '删除',
            onClick: handleDelete.bind(null, record),
            // 根据业务控制是否显示 isShow 和 auth 是并且关系
            // ifShow: () => {
            //   return true;
            // },
            // // 根据权限控制是否显示: 有权限，会显示，支持多个
            // auth: ['basic_list'],
          },
        ],
      });
    },
  });

  const [register, { getFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 120,
    schemas,
    collapsedRows: 2,
  });

  function addTable() {
    showModal.value = true;
  }
  // 滑块切换添加权限状态
  function handleChange(value: boolean) {
    console.log(value);
  }
const total=ref(0)
  const loadDataTable = async (res) => {
     const data= await getWorkshopList({ ...getFieldsValue(), ...res,unit:route.query.id });
       total.value=data.list.total
        data['data'] = data.list.data;
        data['last_page'] = data.list.last_page;
        data['total'] = data.list.total;
        return data
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }
  const goWorkshop = () => {
    router.push('workshop');
  };

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    router.push({ name: 'basic-info', params: { id: record.id } });
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
  const goUnits = () => {
    router.push('/units');
  };
</script>

<style lang="less" scoped></style>
